<?php
class Duomai{
    //橙劵

    //媒体id 263566
    private $duomai_id = '';
    private $hash_android='';


    public function initialize($duomai)
    {
        $this->duomai_id = $duomai['duomai_id'];
        $this->hash_android = $duomai['hash_android'];
        parent::initialize();
    }


    private function get_commonurl($url,$params=[]){
        $params_str = http_build_query($params);
        $url="{$url}?{$params_str}";
        $xmls = file_get_contents($url);
        $xml =simplexml_load_string($xmls);
        $xmljson= json_encode($xml);
        $xml=json_decode($xmljson,true);
        return $xml;
    }


    /**
     * 查询所有有效广告计划接口
     */
    public function goodser_plan(){
        $params['hash']=$this->hash_android;
        $params['action']='getAvalid';
        $url='https://www.duomai.com/api/ads.php';
        $result = $this->get_commonurl($url,$params);
        $count_result = count($result)-1;
        $new_result=[];
        for($i=0;$i< $count_result;$i++){
            $new_result[$i]=$result['fix_'.$i];
        }
        return $new_result;
    }

    /**
     * 商家促销信息查询
     */
    public function goodser_salespromotion(){
        $params['hash']=$this->hash_android;
        $params['action']='getCuxiaoList';
        $url='https://www.duomai.com/api/ads.php';
        $result = $this->get_commonurl($url,$params);
        var_dump($result['articles']);
    }

    /**
     * 商家高佣商品查询
     */
    public function highcommission(){
        $params['hash']=$this->hash_android;
        $params['action']='getHighProductions';
        $url='https://www.duomai.com/api/ads.php';
        $result = $this->get_commonurl($url,$params);
        var_dump($result);
    }


    /**
     * 获取推广链接
     */
    public function gettopromote($h_params){
        $params['site_id']=$this->duomai_id;
        //$params['aid ']=$h_params['aid'];
        $params['t']=$h_params['site_url'];
        $params['euid']=$h_params['user_id'];
        ksort($params);
        $url='http://c.duomai.com/track.php';
        $params_str = http_build_query($params);
        $url="{$url}?{$params_str}";
        return $url;
    }


    //京东推广链接获取
    public function jdtopromoteurl($h_params){
        $params['hash']=$this->hash_android;
        $params['site_id']=$this->duomai_id;
        //$params['aid']=$h_params['aid'];
        $params['euid']=$h_params['user_id'];
        if(isset($h_params['site_url'])) {
            $params['t'] = urlencode($h_params['site_url']);
        }
        if(isset($h_params['skuid'])) {
            $params['skuid'] = $h_params['skuid'];
        }
        $url='http://c.duomai.com/jd.raw.php';
        $params_str = http_build_query($params);
        $url="{$url}?{$params_str}";
        return $url;
    }


    //拼多多推广链接获取
    //推广拼多多地址（UrlEncode编码)可以是商品地址，活动地址。
    public function pddtopromoteurl($h_params){
        $params['hash']=$this->hash_android;
        $params['site_id']=$this->duomai_id;
        $params['aid']=$h_params['aid'];
        $params['euid']=$h_params['user_id'];
        if(isset($h_params['site_url'])) {
            $params['t'] = urlencode($h_params['site_url']);
        }
        $url='http://c.duomai.com/jd.raw.php';
        $params_str = http_build_query($params);
        $url="{$url}?{$params_str}";
        $xmls = file_get_contents($url);
        var_dump(json_decode($xmls,true));
    }


    //订单详情明细查询
    public function order_detail_info($h_params){
        $params['hash']=$this->hash_android;
        $params['action']='query_detail';
        $params['order_sn']=$h_params['order_sn'];
        $params['format']='json';
        $url='https://www.duomai.com/api/order.php';
        $params_str = http_build_query($params);
        $url="{$url}?{$params_str}";
        $xmls = file_get_contents($url);
        $result = json_decode($xmls,true);
        if(is_array($result)){
            return ['code'=>true,'date'=>$result];
        }else{
            return ['code'=>false,'date'=>'没有找到订单'];
        }
    }


    //饿了么推广链接
    public function elm($h_params){
        $params['site_id']=$this->duomai_id;
        $params['euid']=$h_params['user_id'];
        $url='https://c.duomai.com/eleme.php';
        $params_str = http_build_query($params);
        $url="{$url}?{$params_str}";
        $xmls = file_get_contents($url);
        $result = json_decode($xmls,true);
        if($result['code']==400){
            return ['code'=>false,'data'=>$result['message']];
        }else{
            return ['code'=>true,'data'=>$result];
        }
    }


}
